App({
  //获取服务器地址
  getURL: function() {
    var type_ = 'prod'
    // 测试环境
    if (type_ == 'dev') {
      return "";
    }
    //生产环境
    else if (type_ == 'prod') {
      return "https://forestart.top/api/";
    }
  },
  onLaunch() {
    
  },
  setUserInfo(data, name) {
    let userInfo = this.globalData.userInfo
    userInfo[name] = data
    this.globalData.userInfo=userInfo;
    wx.setStorageSync('userInfo', userInfo)
  },
  setAllInfo(data) {
    this.globalData.userInfo=data;
    wx.setStorageSync('userInfo', data)
  },
  getToken() {
    var that = this;
    wx.request({
      url: that.getURL() + '/book/wechat/v1/getToken',
      method: 'GET',
      data: {},
      success(res) {
        console.log(res)
        that.globalData.token=res.data.result;
        wx.setStorageSync('token', res.data.result)
      },
      fail(err) {
        wx.showToast({
          title: '请求服务器失败',
          icon: 'none',
          duration: 2000
        })
      }
    })
  },
  onShow(){
    console.log('onshow')
    // 展示本地存储能力
    var that = this;
    var openId=wx.getStorageSync('openId');
    var phone=wx.getStorageSync('phone');
    var userInfo=wx.getStorageSync('userInfo');
    // var getToken=wx.getStorageSync('getToken');
    // if (getToken) {
    //   this.globalData.getToken = getToken;
    // }
    if(!userInfo || !phone ||!openId){
      //如果本地存储没有openId则获取openid
      if(!openId){
        wx.login({
          success: res => {
            // 发送 res.code 到后台换取 openId, sessionKey, unionId
            that.getOpenID(res.code);
            // that.getToken()
          },
          fail(res){
            console.log(res)
            wx.showToast({
              title: '获取用户信息失败',
              icon: 'none',
              duration: 2000
            })
          }
        })
      } 
      else{
        this.globalData.openId=openId;
        //没有手机号或者用户信息，就跳转至登录页面
        if(!phone || !userInfo){
          wx.reLaunch({
            url: './pages/login/login'
          })
        }
      } 
      
    }else{
      this.globalData.openId = openId;
      this.globalData.phone = phone;
      this.globalData.userInfo = userInfo;
    }
  },
  //小程序通过code获取openId
  getOpenID(code){
    var that = this;
    var openId=wx.getStorageSync('openId');
    var phone=wx.getStorageSync('phone');
    var userInfo=wx.getStorageSync('userInfo');
    wx.request({
      url: that.getURL()+'/book/wechat/v1/getOpenId', 
      method: 'GET', // 请求方法
      data: {
        code:code
      },
      header: {
      'content-type': 'application/json' // 默认值
      },
      success(res) {
        if(res.data.code == 200){
          that.globalData.openId=res.data.result;
          wx.setStorageSync('openId', res.data.result)
          //没有手机号或者用户信息，就跳转至登录页面
          if(!phone || !userInfo){
            wx.reLaunch({
              url: './pages/login/login'
            })
          }
        }else{
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 2000
          })
        }
      },
      fail(res) {
        console.log(res)
        wx.showToast({
          title: '请求服务器失败',
          icon: 'none',
          duration: 2000
        })
      }
    });
  },
  globalData: {
    userInfo: {},
    phone: "",
    openId:"",
  },
  onLoad(query) {
    const q = decodeURIComponent(query.q) // 获取到二维码原始链接内容
    const scancode_time = parseInt(query.scancode_time) // 获取用户扫码时间 UNIX 时间戳
    console.log(q)
  }
})
